package com.ruoyi.common.core.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.Collections;
import java.util.List;

/**
 * @Author: DaoZhuang
 * @Date: 2020/12/12
 */
public class PageData<T> implements IPage<T> {

    private static final long serialVersionUID = 8545996863226528798L;
    protected Integer code = 200;   //返回code码  默认是成功
    protected String msg = "Success";    //返回信息 默认是成功
    protected Long total;  //数据总条数
    protected Long pageNum;  //当前页 默认是 第1页
    protected Long pageSize;  //每页条数 默认是 30条
    protected List<T> rows;


    public PageData() {
        this.rows = Collections.emptyList();
        this.total = 0L;
        this.pageSize = 10L;
        this.pageNum = 1L;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Long getPageNum() {
        return pageNum;
    }

    public void setPageNum(Long pageNum) {
        this.pageNum = pageNum;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getRows() {
        return rows;
    }

    @Override
    @JsonIgnore
    public long getPages() {
        if (this.getSize() == 0L) {
            return 0L;
        } else {
            long pages = this.getTotal() / this.getSize();
            if (this.getTotal() % this.getSize() != 0L) {
                ++pages;
            }
            return pages;
        }
    }

    @Override
    public List<OrderItem> orders() {
        return null;
    }

    @Override
    @JsonIgnore
    public List<T> getRecords() {
        return this.rows;
    }

    @Override
    public IPage<T> setRecords(List<T> records) {
        this.rows = records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public IPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    @JsonIgnore
    public long getSize() {
        return this.pageSize;
    }

    @Override
    public IPage<T> setSize(long size) {
        this.pageSize = size;
        return this;
    }

    @Override
    @JsonIgnore
    public long getCurrent() {
        return this.pageNum;
    }

    @Override
    public IPage<T> setCurrent(long current) {
        this.pageNum = current;
        return this;
    }

}